package com.yutel.system.struts.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yutel.component.ExceptionUtils;
import com.yutel.system.repository.ExtComm;
import com.yutel.system.repository.ExtPanel;
import com.yutel.system.service.IExtPanelService;
import com.yutel.system.struts.core.BaseAction;
import com.yutel.system.struts.core.Page;

public class ExtPanelAction extends BaseAction {

	public String tree() {
		ExtPanel pan=new ExtPanel();
		String id = (String) getRequest().getParameter("id");
		if(id!=null && !"".equals(id)){
			pan.setId(Integer.valueOf(id));
		}
		List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
		try {
			List<ExtPanel> list = extPanelService.list(pan);
			for (ExtPanel sc : list) {
				Map<String, Object> tree = new HashMap<String, Object>();
				tree.put("id", sc.getId());
				tree.put("text",sc.getName());
				tree.put("type", sc.getType());
				tree.put("tableId",sc.getTableId());
				if(extPanelService.list(sc).size()>0){
					tree.put("leaf",false);
				} else {
					tree.put("leaf",true);
				}
				retList.add(tree);
			}
			treeList=retList;
		} catch (Exception e) {
			msg=ExceptionUtils.handleException(e);
		}
		success=true;
		return SUCCESS;
	}
	public String view() {
		try{
			ExtPanel pan=new ExtPanel();
			String id = (String) getRequest().getParameter("id");
			if(id!=null && !"".equals(id)){
				pan.setId(Integer.valueOf(id));
			}
			ext=extPanelService.view(pan);
			success=true;
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String save() {
		try{
			if(ext.getId()!=null && ext.getId()>0){
				extPanelService.edit(ext);
				msg="保存成功";
			} else {
				extPanelService.add(ext);
				msg="添加成功";
			}
			success=true;
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String drop() {
		try{
			ExtPanel pan=new ExtPanel();
			String id = (String) getRequest().getParameter("id");
			if(id!=null && !"".equals(id)){
				pan.setId(Integer.valueOf(id));
			}
			extPanelService.del(pan);
			success=true;
			msg="删除成功";
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String comm() {
		try{
			ExtPanel pan=new ExtPanel();
			String id = (String) getRequest().getParameter("id");
			if(id!=null && !"".equals(id)){
				pan.setId(Integer.valueOf(id));
			}
			page.setRoot(extPanelService.commByPage(pan));
			msg="添加成功";
			success=true;
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String saveComm() {
		try{
			if(comm.getId()!=null && comm.getId()>0){
				extPanelService.editComm(comm);
				msg="保存成功";
			} else {
				extPanelService.addComm(comm);
				msg="添加成功";
			}
			success=true;
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String dropComm() {
		try{
			String commIds = getRequest().getParameter("commIds");
			if(commIds!=null && !"".equals(commIds)){
				commIds=commIds.substring(0,commIds.length()-1);
			}
			extPanelService.delComm(commIds);
			success=true;
			msg="删除成功";
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	public String viewComm() {
		try{
			ExtComm com=new ExtComm();
			String id = (String) getRequest().getParameter("id");
			if(id!=null && !"".equals(id)){
				com.setId(Integer.valueOf(id));
			}
			comm=extPanelService.viewComm(com);
			success=true;
			msg="查看成功";
			return SUCCESS;
		} catch(Exception e){
			msg=ExceptionUtils.handleException(e);
			return ERROR;
		}
	}
	
	private static final long serialVersionUID = 1323234L;
	private IExtPanelService extPanelService;
	List<Map<String, Object>> treeList = null;
	private Page page=new Page();
	private ExtPanel ext=new ExtPanel();
	private ExtComm comm=new ExtComm();
	public ExtComm getComm() {
		return comm;
	}
	public void setComm(ExtComm comm) {
		this.comm = comm;
	}
	private boolean success;
	private String msg;
	public List<Map<String, Object>> getTreeList() {
		return treeList;
	}
	public void setTreeList(List<Map<String, Object>> treeList) {
		this.treeList = treeList;
	}

	public void setExtPanelService(IExtPanelService extPanelService) {
		this.extPanelService = extPanelService;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public ExtPanel getExt() {
		return ext;
	}
	public void setExt(ExtPanel ext) {
		this.ext = ext;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
}